<?php
  class JobLogAction extends CommonAction{
	    //默认工作日志显示页面
	    public function jobLog(){
		   //调取相应用户的工作日志
		   $Uid = $_SESSION['UserId'];
		   //查看当前用户的部门
		   $user = M("employees");
		   $users= $user->where("u_id != {$Uid}")->select();
		   $this->assign("users",$users);
		   
		   $u_info = $user->field("u_department")->where("u_id = {$Uid}")->find();		   
		   //查询此员工是否包含在管理部门
		   $department = M("department");
		   $where['d_name'] = array("like","%管理%");
		   $where['d_id']   = $u_info['u_department'];
		   $d_info = $department->field("d_name")->where($where)->find();
		   import('ORG.Util.page'); //导入分页类(非原TINKPHP分页类)
		   //如果在管理部门的话就输出所有员工日志，否则只单独输出员工自己的日志
		   $jobLog = M("joblog");
		   if($d_info){
			  $this->assign("r",1);
			  //分页处理
			  $count = $jobLog->order("j_date desc")->count();
			  $page = new Page($count,10);
			  
			  $j_info = $jobLog->order("j_date desc")->limit($page->limit)->select();
			  //将j_uid号换成相应的员工姓名 
			  for($i=0;$i<count($j_info);$i++){
				  $name = $user->field("u_id,u_name")->where("u_id = {$j_info[$i]['j_uid']}")->find();
				  $j_info[$i]['j_uid'] = $name['u_name'];  
			  }
		   }else{		
		      //分页处理
			  $count = $jobLog->where("j_uid = {$Uid}")->order("j_date desc")->count();
			  $page = new Page($count,10);   
		      $j_info = $jobLog->where("j_uid = {$Uid}")->limit($page->limit)->order("j_date desc")->select();  
			  //将j_uid号换成相应的员工姓名 
			  for($i=0;$i<count($j_info);$i++){
				  $name = $user->field("u_id,u_name")->where("u_id = {$j_info[$i]['j_uid']}")->find();
				  $j_info[$i]['j_uid'] = $name['u_name'];  
			  }
		   }
		   $this->assign("j_info",$j_info);
		   $this->assign("fpage",$page->fpage(1,4,5,6,0,3));
		   
		   //根据日期进行查询
		   if(!empty($_GET['action'])&&($_GET['action']=='query_date')){		
			  $query_date = $_POST['query_date'];
			  if($d_info){  
		      $j_info = $jobLog->order("j_date desc")->select();
			  	for($i=0;$i<count($j_info);$i++){
				  	if($query_date == date("Y-m-d",$j_info[$i]['j_date'])){
					   $j_info2[] = $j_info[$i];
				  	}
				 } 
				 //将j_uid号换成相应的员工姓名 
			     for($i=0;$i<count($j_info2);$i++){
				  $name = $user->field("u_id,u_name")->where("u_id = {$j_info[$i]['j_uid']}")->find();
				  $j_info2[$i]['j_uid'] = $name['u_name'];  
			     }
			  }else{
				$j_info = $jobLog->where("j_uid = {$Uid}")->order("j_date desc")->select();
			  	for($i=0;$i<count($j_info);$i++){
				  	if($query_date == date("Y-m-d",$j_info[$i]['j_date'])){
					 $j_info2[] = $j_info[$i];
				  	}
				 } 
				 //将j_uid号换成相应的员工姓名 
			     for($i=0;$i<count($j_info2);$i++){
				  $name = $user->field("u_id,u_name")->where("u_id = {$j_info[$i]['j_uid']}")->find();
				  $j_info2[$i]['j_uid'] = $name['u_name'];  
			     }  
			  }
			  $this->assign("j_info",$j_info2);
			  $this->assign("fpage","");
		   }
		   
		   
		   //根据姓名进行查询
		   if(!empty($_GET['action'])&&($_GET['action']=='query_name')){		
			  $query_name = $_POST['query_name'];
			  if($d_info){
		      $j_info = $jobLog->order("j_date desc")->where("j_uid = {$query_name}")->select();	     
				 //将j_uid号换成相应的员工姓名 
			      for($i=0;$i<count($j_info);$i++){
				  $name = $user->field("u_id,u_name")->where("u_id = {$j_info[$i]['j_uid']}")->find();
				  $j_info[$i]['j_uid'] = $name['u_name'];  
			     }
			  }else{
				$this->error("对不起,您没有这个权限");
			  }
			  $this->assign("j_info",$j_info);
			  $this->assign("fpage","");
		   }
	   
		   $this->display();	
		}
		
		//添加今日工作日志页面
		public function add_jobLog(){
		   $time = time();
		   $this->assign("time",$time);
		   $this->Online($_SESSION['UserId']);		   
		   $this->display();	
		}
		
		//添加今日工作日志处理
		public function do_add_jobLog(){
		   $Uid = $_SESSION['UserId'];
		   $jobLog = M("joblog");
		   
		   $time = time();
		   $date_info = date("Y-m-d",$time);
		   
		   $j_date = $jobLog->field("j_date")->where("j_uid = {$Uid}")->select();

		   for($i=0;$i<count($j_date);$i++){
			   $r_date_info = date("Y-m-d",$j_date[$i]['j_date']);  
			   if($date_info == $r_date_info){
				  $this->error("对不起,今天你已经创建了日志");   
			   }
		   }
		   
		   $data['j_uid'] = $Uid;
		   $data['j_date'] = time();
		   $data['j_content'] = nl2br($_POST['j_content']);
		   $data['j_result']  = $_POST['j_result'];
		   $data['j_note'] = nl2br($_POST['j_note']);
  		   
		   if($jobLog->add($data)){
			        $this->Online($Uid);
			        $this->Log($Uid,"创建工作日志",1);
			  		$this->success("日志添加成功",U("JobLog/jobLog"));
		   }else{
			        $this->Online($Uid);
			        $this->Log($Uid,"创建工作日志",0);
		      		$this->error("对不起,日志添加失败！！！");	 
		   }	   		   
		}
		
		//查看工作日志
		public function look_jobLog(){	
		   $Uid = $_SESSION['UserId'];
		   $j_id = $_GET['j_id'];
		   
		   $jobLog = M("joblog");
		   $j_info = $jobLog->find($j_id);
		   $this->assign("j_info",$j_info);
		   $this->Online($_SESSION['UserId']);
		   $this->display();	
		}
		
		//对员工日志进行评价
		public function review_joblog(){
		   $j_id = $_GET['j_id'];
		   
		   $jobLog = M("joblog");
		   $j_info = $jobLog->find($j_id);
		   if($j_info['j_uid'] == $_SESSION['UserId']){
			  $this->error("对不起,不能对自身进行评价");   
		   }
		   
		   $this->assign("j_info",$j_info);
		   $this->Online($_SESSION['UserId']);
		   $this->display();	
		}
		
		//评价处理
		public function do_review_joblog(){
		   $Uid = $_SESSION['UserId'];
		   
		   //根据Uid获取到评论人的名字
		   $user = M("employees");
		   $name = $user->field("u_name")->find($Uid);
		   
		   $j_id = $_POST['j_id'];
		   $data['j_boss_review'] = $_POST['j_boss_review'];
		   $data['j_rname'] = $name['u_name'];
		   
		   $jobLog = M("joblog");
		   if($jobLog->where("j_id = {$j_id}")->save($data)){
			   $this->Online($Uid);
			   $this->Log($Uid,"评价工作日志",1);
			   $this->success("评价提交成功!!!",U("JobLog/jobLog"));  
		   }else{
			   $this->Online($Uid);
			   $this->Log($Uid,"评价工作日志",0);
			   $this->error("评价失败,请重新尝试"); 
		   }		   	
		}
		
		//查看上司评价
		public function look_review(){
			$Uid = $_SESSION['UserId'];
			$j_id = $_GET['j_id'];			
			$jobLog = M("joblog");
			$save['j_look_review'] = 1;
		    $jobLog->where("j_id = {$j_id} and j_uid = {$Uid}")->save($save);
			$j_info = $jobLog->find($j_id);
			$this->assign("j_info",$j_info);
			
			$this->Online($_SESSION['UserId']);
		   	$this->display();
		}
		
		//修改日志
		public function edit_jobLog(){
		    $Uid = $_SESSION['UserId'];	
		    $j_id = $_GET['j_id'];
			
			$jobLog = M("joblog");
			$j_info = $jobLog->find($j_id);
			$j_uid = $j_info['j_uid'];
			//得到日志的发布日期
			$j_date = $j_info['j_date'];
			$date_info = date("Y-m-d",$j_date);
			//获取今天的日期
			$time = time();
			$date_info2 = date("Y-m-d",$time);
			//判断是否是当天的日志
			if($date_info!=$date_info2){
			   $this->error("对不起,只能修改当天的日志");	
			}
			
			//如果该条日志不是自己的就不允许修改（主要针对领导不能修改员工日志）
			if($Uid!=$j_uid){
			   $this->error("对不起,不能修改别人的工作日志");	
			}
			
			$this->assign("j_info",$j_info);
			$this->Online($_SESSION['UserId']);
			$this->display();
		}
		
		//修改日志处理
		public function do_edit_jobLog(){
		    $j_id = $_POST['j_id'];	
			$jobLog = M("joblog");
			
			$data['j_content'] = $_POST['j_content'];
			$data['j_result']  = $_POST['j_result'];
			$data['j_note']    = $_POST['j_note'];
			
			if($jobLog->where("j_id = {$j_id}")->save($data)){
			    $this->Online($Uid);
			    $this->Log($Uid,"修改工作日志",1);
			    $this->success("日志修改成功",U("JobLog/jobLog"));	
			}else{
				$this->Online($Uid);
			    $this->Log($Uid,"修改工作日志",0);
				$this->error("对不起,日志修改失败");
			}
		}
  }
?>
